﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using DevExpress.UserSkins;
using DevExpress.XtraBars.Helpers;
using DevExpress.Skins;
using Gui.GuiPro;
using _3lop.GUI;

namespace DMU
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public frmMain()
        {
            InitializeComponent();
        }
        Service sv = new Service();
        private void frmMain_Load(object sender, EventArgs e)
        {

          //foreach (SkinContainer skin in SkinManager.Default.Skins)
            //{

            //    BarButtonItem a = new BarButtonItem(barManager1, skin.SkinName);
            //    barSubItem1.AddItem(a);
            //}

        }

        public void createTab(XtraUserControl uc, string tabName)
        {
            int tabIndex=checkExistTab(tabName);
            if (tabIndex == -1)
            {
                XtraTabPage tp = new XtraTabPage();
                tp.Text = tabName;
                
                uc.Dock = DockStyle.Fill;
                tp.Controls.Add(uc);
                
                xtTabMain.TabPages.Add(tp);
                xtTabMain.SelectedTabPage = tp;

                uc.Show();
            }
            else
            {
                xtTabMain.SelectedTabPageIndex = tabIndex;
            }
            

        }

        public void createTab(Form frm, string tabName)
        {
            int tabIndex = checkExistTab(tabName);
            if (tabIndex == -1)
            {
                XtraTabPage tp = new XtraTabPage();
                tp.Text = tabName;
                frm.Dock = DockStyle.Fill;
                frm.TopLevel = false;
                frm.FormBorderStyle = FormBorderStyle.None;
                tp.Controls.Add(frm);

                xtTabMain.TabPages.Add(tp);
                xtTabMain.SelectedTabPage = tp;
                frm.Show();

            }
            else
            {
                xtTabMain.SelectedTabPageIndex = tabIndex;
            }


        }

        private int checkExistTab(string tabName)
        {
            int rt = -1;
            for(int i=0;i<xtTabMain.TabPages.Count;i++)
            {
                if (xtTabMain.TabPages[i].Text== tabName)
                {
                    rt = i;
                    break;
                }
            }
            return rt;
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            //createTab(new XtraUserControl1(), "Tabhienthi");
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
        }

        private void xtTabMain_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.XtraTabControl tabControl = sender as DevExpress.XtraTab.XtraTabControl;
            DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs arg = e as DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs;
            (arg.Page as DevExpress.XtraTab.XtraTabPage).Dispose();
        }

        private void btnNganh_ItemClick(object sender, ItemClickEventArgs e)
        {
            //createTab(new Gui.uc_Kho(), "Thông tin ngành học");
        }

        private void btnHangHoa_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.ucHoangHoa(xtTabMain), "Thông Tin Hàng Hóa");
        }

        private void barSubItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            //DevExpress.LookAndFeel.UserLookAndFeel.Default.SkinName = "Office 2010 Black";
            
        }

        private void btnChangePas_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmChangPas frm = new frmChangPas();
            frm.ShowDialog();
        }

        private void btnConfig_ItemClick(object sender, ItemClickEventArgs e)
        {
           /* Gui.GuiPro.frmThietLap frm = new Gui.GuiPro.frmThietLap();
            frm.ShowDialog();*/
        }

        private void btnLoaiDT_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.ucLoaiDT(), "Loại đào tạo");
        }

        private void btnLop_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.ucLop(xtTabMain), "Thông tin lớp");
        }

        private void btnMonHoc_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.uc_Kho(), "Thông tin Kho Hàng");
        }

        private void btnPhong_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.ucKhachHang(), "Thông tin Khách Hàng");
        }

        private void btnBoMon_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.uc_NhaCungCap(), "Thông tin Nhà cung cấp");
        }

        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                Gui.GuiPro.frmThietLap frm = new Gui.GuiPro.frmThietLap();
                frm.ShowDialog();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lbGio.Caption = "Thời gian : " + DateTime.Now.ToString();
        }

        private void btnGV_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.ucNhanVien(xtTabMain), "Thông tin nhân viên"); 
        }

        private void btnThoiKhoaBieu_ItemClick(object sender, ItemClickEventArgs e)
        {
            
        }

        private void btnChuyenDoi_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.GuiPro.frmChuyenDoiThongTin(), "Thông tin chuyển đổi");
        }

        private void xtTabMain_Click(object sender, EventArgs e)
        {

        }

        private void textNgayGio_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void frmMain_Activated(object sender, EventArgs e)
        {
            ribbon.Enabled = Service.isLogin;
            
            if (!Service.isLogin)
            {
                Service.isLogin = true;
                Gui.frmDangNhap fdn = new Gui.frmDangNhap();
                fdn.ShowDialog();
            }
            lbMaCB.Caption = Service.username;
            lbTenCB.Caption = Service.fullname;
        }

        private void xtTabMain_ControlAdded(object sender, ControlEventArgs e)
        {
            xtTabMain.SelectedTabPageIndex = xtTabMain.TabPages.Count - 1;
        }

        private void btnKhoa_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmLock frm = new frmLock();
            frm.ShowDialog();
        }

        private void btnDangXuat_ItemClick(object sender, ItemClickEventArgs e)
        {
            Service.isLogin = false;
            EventArgs et=new EventArgs();
            frmMain_Load(sender, et);
        }

        private void btnRPTGiangVien_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.RPT.ucRPTGiangVien(), "Tình hình giảng dạy");
        }

        private void btnRPTSinhVien_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.RPT.ucRPTSinhVienTheoMon(), "Theo giỏi sinh viên");
        }

        private void ribbon_Click(object sender, EventArgs e)
        {

        }

        private void btnLoaiHangHoa_ItemClick(object sender, ItemClickEventArgs e)
        {
            createTab(new Gui.ucLoaiHang(), "Thông Tin Loại Hàng Hóa");
        }

        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database='QLKhoHang';Integrated Security=true");
            sfdsaoluu.Filter="DataBase(*.bak)|*.bak";
            sfdsaoluu.Title = "Chọn đường dẫn..";
            sfdsaoluu.FileName = "QLKhoHang(" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + ")";
            sfdsaoluu.DefaultExt = "*.bak";
            if (sfdsaoluu.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string sqlbackup = "Backup Database QLKhoHang to Disk='" + sfdsaoluu.FileName + "' WITH DESCRIPTION = 'QLKhoHang FULL Backup'";
                    SqlCommand cmd = new SqlCommand(sqlbackup, con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Sao Lưu Thành Công!","Thành Công",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                catch(SqlException et)
                {
                    MessageBox.Show(et.Message);
                }
                catch(Exception ec)
                {
                    MessageBox.Show(ec.Message);
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                    con.Dispose();
                }
            }
        }

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Database='QLKhoHang';Integrated Security=true");
            ofdrestore.Filter = "Database(*.bak)|*.bak";
            ofdrestore.Title = "Chọn đường dẫn...";
            if (ofdrestore.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string sqlrestore = "Restore Database QLKhoHang from Disk='" + ofdrestore.FileName + "' WITH NORECOVERY'";
                    SqlCommand cmd = new SqlCommand(sqlrestore, con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Thanh Cong");
                }
                catch (SqlException et)
                {
                    MessageBox.Show(et.Message+"loi1");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message+"loi2");
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                    con.Dispose();
                }
            }
        }
    }
}